package com.etc.user.controller;

import com.etc.entity.*;
import com.etc.entity.Cartitem;
import com.etc.service.*;
import com.etc.service.impl.FlashOrderServiceImpl;
import com.etc.utils.CommonMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 文件名：com.etc.user.controller
 * 描述：
 * 作者：高文乾
 * 时间： 2019/5/28 9:46.
 */
@Controller
@RequestMapping(value = "user.action")
public class CartitemController {
    @Autowired
    private  OrderListService orderListService;

    @Autowired
   private OrderService orderService;

    @Autowired
    private GoodsPictureService goodsPictureService;

    @Autowired
    private FlashOrderServiceImpl flashOrderService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CartService cartService;

    @Autowired
    private CartitemService cartitemService;

    @GetMapping(value = "toShopCart")
    public String toShopCart(Integer userid, Model model){
        Integer cartid = cartService.getCartidByUserid(userid);
        List<Cartitem> list = cartitemService.listCartitemByCartId(cartid);
        model.addAttribute("list",list);
        return "user/shopCart";
    }

    @RequestMapping(value = "getGoodsid", method=RequestMethod.POST)
    @ResponseBody
    public CommonMessage getGoodsid( Integer goodsid, Integer goodsnum, Integer userid){
        CommonMessage cm=new CommonMessage();
        cm.setMsg("操作失败");
        //通过goodsid查询goods
        Goods goods = goodsService.getGoodsByid(goodsid);
        //通过商品id查询该商品的图片首页id
        Integer pictureid = goodsPictureService.getGoodspictureByGid(goodsid).getPictureid();
        //判断用户是否已经有了购物车，没有就创建一个，有就执行事务
        Integer cartid = cartService.getCartidByUserid(userid);
        System.out.println("cartid"+cartid);
        if (cartid==null){
            //先添加一个购物车,购物车里的商品总数量为0
            Cart cart = new Cart(null,userid,0.0);
            cartService.insertCart(cart);
            //再添加购物车项
            Cartitem cartitem = new Cartitem(null,goodsid,pictureid,goodsnum,cart.getCartid());
            cartitemService.insertCartitem(cartitem);
            cm.setMsg("操作成功");
        }else {
            //已经有购物车了
            //判断该goodsid在Cartitem中是否已经存在，如果不存在则添加Cartid,如果存在修改商品数量；
            List<Cartitem> list = cartitemService.mySelectCartitemByCartId(cartid);
            list.forEach(System.out::println);
            //定义一个布尔，如果结果为真，说明for循环里的更新语句有执行
            boolean flag=false;
            //遍历购物车中的购物项
            for (Cartitem cartitem:list) {
                if(cartitem.getGoodsid()==goodsid){
                    System.out.println("for循环"+goodsid);
                    //该商品在购物车中已将存在了，修改商品数量
                    Integer  goodsnum1 = cartitem.getGoodsnum()+goodsnum;
                    cartitem.setGoodsnum(goodsnum1);
                    //boolean flag = cartitemService.updatGoodsnum(goodsnum,goodsid,cartid);
                    flag = cartitemService.updateByPrimaryKeySelective(cartitem);
                    break;
                }
            }
            //flag为真，说明以上updata语句有执行

            if(flag){
                cm.setMsg("操作成功");
            }else{
                //flag为假，说明以上updata语句没有执行，购物车项表里面没有这个商品，则插入
                //购物车中不存在该商品，添加到购物车中
                Cartitem cartitem02 = new Cartitem(null,goodsid,goodsnum,cartid,pictureid);
                cartitemService.insertCartitem(cartitem02);
                cm.setMsg("操作成功");
            }
        }
        return cm;


    }

    /**

     *用户在购物车里执行减号的动作
     * @param cartitemid 购物车项的id
     * @return
     */
    @RequestMapping(value = "doLessGoodsNum")
    @ResponseBody
    public CommonMessage doLessGoodsNum( Integer cartitemid){
        CommonMessage cm=new CommonMessage();
        boolean flag=cartitemService.updateByGoodsLessNum(cartitemid);
        if(flag){
            cm.setMsg("成功");
        }else{
            cm.setMsg("失败");
        }

        return cm;
    }

    /**
     *用户在购物车里执行加号的动作
     * @param cartitemid 购物车项的id
     * @return
     */
    @RequestMapping(value = "doAddGoodsNum")
    @ResponseBody
    public CommonMessage doAddGoodsNum( Integer cartitemid){
        CommonMessage cm=new CommonMessage();
        boolean flag=cartitemService.updateByGoodsAddNum(cartitemid);
        if(flag){
            cm.setMsg("成功");
        }else{
            cm.setMsg("失败");
        }
        return cm;
    }

    /**
     *用户在购物车里执行加号的动作
     * @param order 购物车的id
     * @return
     */
    @RequestMapping(value = "doJieSuan", method=RequestMethod.POST)
    @ResponseBody
    public CommonMessage doJieSuan( Integer order){
        CommonMessage cm=new CommonMessage();
        System.out.println(order);
        //增加订单
        //获取生成的订单号
        String ordernum=flashOrderService.getOrderNum("OO");
         Cart cart=cartService.getCarById(order);
         //得到这个订单总共需要多少钱doLessGoodsNum
        Double ordersalary=cartService.countOrderSalary(order);
        Orders orders=new Orders(null,ordernum, cart.getUserid(),ordersalary, new Date(),1);
       //添加总的订单进去
        boolean flag1= orderService.insertSelective(orders);
        System.out.println(flag1);
        //得到添加成功后的订单id
       Integer orederid=orderService.selectByOrderNum(ordernum).getOrderid();
        //得到该购物车的购物车项
        List<Cartitem> listGoods=cartitemService.mySelectCartitemByCartId(order);
        //执行插入语句
        for (Cartitem cartitem:listGoods) {
            orderListService.insertSelective(new OrderList(null,orederid,cartitem.getGoodsid(),cartitem.getGoodsnum()));
        }
        if(flag1){
            //Integer cartid =cartService.getCartidByUserid(userid);
            boolean flag = cartitemService.deletCartitemByCartid(order);
           /* if (flag){
                boolean isok = cartService.deleteCartBycartid(cartid);
            }*/
        }
        return cm;
    }

    /**
     *删除购物项
     * @param cartitemid
     * @return
     */
    @RequestMapping(value = "deleteCartitem",method = RequestMethod.POST)
    @ResponseBody
    public CommonMessage deletCartitem(Integer cartitemid){
        System.out.println(cartitemid);

        CommonMessage cm = new CommonMessage();
        cm.setMsg("操作失败");

       boolean flag =  cartitemService.deletCartitemById(cartitemid);
       if (flag){
           cm.setMsg("操作成功");
       }

       return cm;
    }


    /**
     *购物车页面生成订单的方法
     * @param order 购物车项的id
     * @return 支付界面
     *//*
    @RequestMapping(value = "toPay")
    public String toPay( Integer order ,Integer userid ,Model model){

        //增加订单
        //获取生成的订单号
        String ordernum=flashOrderService.getOrderNum("OO");
        Cart cart=cartService.getCarById(order);
        //得到这个订单总共需要多少钱
        Double ordersalary=cartService.countOrderSalary(order);
        Orders orders=new Orders(null,ordernum, cart.getUserid(),ordersalary, new Date(),0);
        //添加总的订单进去
        boolean flag1= orderService.insertSelective(orders);
        //得到添加成功后的订单id
        Integer orederid=orderService.selectByOrderNum(ordernum).getOrderid();
        //得到该购物车的购物车项
        List<Cartitem> listGoods=cartitemService.mySelectCartitemByCartId(order);
        //执行插入语句
        for (Cartitem cartitem:listGoods) {
            orderListService.insertSelective(new OrderList(null,orederid,cartitem.getGoodsid(),cartitem.getGoodsnum()));
        }
        if(flag1){
            Integer cartid =cartService.getCartidByUserid(userid);
           boolean flag = cartitemService.deletCartitemByCartid(cartid);
           if (flag){
               boolean isok = cartService.deleteCartBycartid(cartid);
           }
        }

        Orders orders1 = orderService.getOrdersByOrderid(orederid);
        model.addAttribute("orders",orders1);
        return "user/pay";
    }*/
}
